package com.jrfid.smartcabinet.demo.ui.login

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.adapter.StatefulAdapter
import androidx.viewpager2.widget.ViewPager2
import com.jrfid.smartcabinet.demo.R
import com.jrfid.smartcabinet.demo.base.BaseFragment
import com.jrfid.smartcabinet.demo.databinding.*
import com.jrfid.smartcabinet.demo.ui.login.account.LoginByAccountFragment
import com.jrfid.smartcabinet.demo.ui.login.face.LoginByFaceFragment
import com.jrfid.smartcabinet.demo.ui.login.finger.LoginByFingerFragment
import com.jrfid.smartcabinet.demo.ui.login.nfc.LoginByNfcFragment
import com.jrfid.smartcabinet.demo.view.MRadioGroup

class LoginFragment : BaseFragment<LoginFragmentBinding, LoginViewModel>(LoginViewModel::class.java) {

    companion object {
        fun newInstance() = LoginFragment()
    }

    override fun getLayoutId(): Int = R.layout.login_fragment

    override fun setData() {
        val fragments = arrayListOf(LoginByNfcFragment.newInstance(), LoginByFaceFragment.newInstance(), LoginByAccountFragment.newInstance(), LoginByFingerFragment.newInstance())
        getDataBinding()?.viewPager?.let {
            it.adapter = object : FragmentStateAdapter(this) {
                override fun getItemCount(): Int = fragments.size

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
            it.offscreenPageLimit = 4
            it.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    getDataBinding()?.radioGroup?.setChecked(position)
                }
            })
        }
        getDataBinding()?.radioGroup?.setOnCheckedChangeListener(object : MRadioGroup.OnCheckedChangeListener {
            override fun onCheckedChange(index: Int) {
                getDataBinding()?.viewPager?.setCurrentItem(index, false)
            }
        })

    }
}